package com.newsplatform.common;

import lombok.Data;

/**
 * 统一响应结果
 * 
 * @author Logan.Li
 */
@Data
public class Result<T> {
    private String msg;
    private String errors;
    private Integer code;
    private T data;

    public Result() {}

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.errors = "";
    }

    public Result(Integer code, String msg, String errors) {
        this.code = code;
        this.msg = msg;
        this.errors = errors;
        this.data = null;
    }

    /**
     * 成功响应
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(20000, "成功", data);
    }

    /**
     * 成功响应（无数据）
     */
    public static <T> Result<T> success() {
        return new Result<>(20000, "成功", null);
    }

    /**
     * 失败响应
     */
    public static <T> Result<T> error(String msg) {
        return new Result<>(50000, msg, "");
    }

    /**
     * 失败响应（带错误详情）
     */
    public static <T> Result<T> error(String msg, String errors) {
        return new Result<>(50000, msg, errors);
    }

    /**
     * 自定义响应
     */
    public static <T> Result<T> build(Integer code, String msg, T data) {
        return new Result<>(code, msg, data);
    }
}
